package com.ziyun.erp.web.modules.freightages.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.freightages.entity.FreightAreaEntity;
import com.ziyun.erp.modules.freightages.entity.FreightTemplateEntity;
import com.ziyun.erp.modules.freightages.entity.SdbEctoolsRegionsEntity;
import com.ziyun.erp.modules.freightages.service.FreightAreaService;
import com.ziyun.erp.modules.freightages.service.FreightTemplateService;
import com.ziyun.erp.modules.freightages.service.SdbEctoolsRegionsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 运费模板表
 *
 * @author 
 * @email 
 * @date 2018-08-10 15:38:32
 */
@RestController
@RequestMapping("freightages/freighttemplate")
public class FreightTemplateController {
    @Autowired
    private FreightTemplateService freightTemplateService;

    @Autowired
    private FreightAreaService freightAreaService;

    @Autowired
    private SdbEctoolsRegionsService sdbEctoolsRegionsService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("freightages:freighttemplate:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",1);
        PageUtils page = freightTemplateService.queryPage(params);
        List<SdbEctoolsRegionsEntity> regionsListRoot = sdbEctoolsRegionsService.findRoots();
        JSONArray jsonArray = new JSONArray();
        for (SdbEctoolsRegionsEntity egions : regionsListRoot) {
            JSONArray jsonArrayChild = new JSONArray();
            JSONObject json = new JSONObject();
            json.put("id", egions.getRegionId());
            json.put("name", egions.getLocalName());
            List<SdbEctoolsRegionsEntity> listChild = sdbEctoolsRegionsService.queryListByParentId(egions.getRegionId());
            for (SdbEctoolsRegionsEntity areaChild : listChild) {
                JSONObject jsonChild = new JSONObject();
                jsonChild.put("id", areaChild.getRegionId());
                jsonChild.put("name", areaChild.getLocalName());
                jsonArrayChild.add(jsonChild);
            }
            json.put("sub", jsonArrayChild);
            jsonArray.add(json);
        }
        return R.ok().put("page", page).put("userData",jsonArray);

    }

    @RequestMapping("/newList")
    public R newList(@RequestBody Map<String, Object> params){
        params.put("sellerId",1);
        return freightTemplateService.newList(params);
//        return R.ok().put("page", page).put("allRegions",allRegions);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{templateId}")
//    @RequiresPermissions("freightages:freighttemplate:info")
    public R info(@PathVariable("templateId") Integer templateId){
        FreightTemplateEntity freightTemplate = freightTemplateService.selectById(templateId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("template_id",templateId);
        map.put("disabled",0);
        List<FreightAreaEntity> freightAreaList = freightAreaService.selectByMap(map);
        return R.ok().put("freightTemplate", freightTemplate).put("freightAreaList",freightAreaList);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("freightages:freighttemplate:save")
    public R save(@RequestBody FreightTemplateEntity freightTemplate){
        freightTemplate.setSellerId(1);
        freightTemplate.setCreateName(UserUtils.getCurentUser().getUsername());
        List<FreightAreaEntity> freightAreaList = freightTemplate.getFreightAreaList();
//        for(int i = 0;i<freightAreaList.size();i++){
//            freightAreaList.get(i).setCreateName(UserUtils.getCurentUser().getUsername());
//        }
        freightTemplateService.insertFreightTemplate(freightTemplate);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("freightages:freighttemplate:update")
    public R update(@RequestBody FreightTemplateEntity freightTemplate){
        freightTemplate.setUpdateName(UserUtils.getCurentUser().getUsername());
        freightTemplate.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        List<FreightAreaEntity> freightAreaList = freightTemplate.getFreightAreaList();
        for(int i = 0;i<freightAreaList.size();i++){
            freightAreaList.get(i).setUpdateName(UserUtils.getCurentUser().getUsername());
            freightAreaList.get(i).setUpdateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        }
        freightTemplateService.updateFreightTemplate(freightTemplate);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("freightages:freighttemplate:delete")
    public R delete(@RequestBody Integer[] templateIds){
        freightTemplateService.flagDelete(templateIds);
        return R.ok();
    }
    /**
     * 保存
     */
    @RequestMapping("/allList")
    public R allList(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("disabled",0);
        map.put("seller_id",1);
        List<FreightTemplateEntity> freightTemplateList = freightTemplateService.selectByMap(map);
        return R.ok().put("freightTemplateList",freightTemplateList);
    }
}
